package com.example.imtoken.modules.main.wallet.manage.admin.details

import android.text.InputType
import com.blankj.utilcode.util.ToastUtils
import com.example.imtoken.R
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.databinding.ActivityAdminDetailsBinding
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog

class AdminDetailsActivity :
    BaseActivity<AdminDetailsViewModel, ActivityAdminDetailsBinding>(AdminDetailsViewModel::class.java) {

    private val key by lazy {
        intent.getStringExtra("key")
    }

    override fun initLayoutId(): Int {
        return R.layout.activity_admin_details
    }

    override fun init() {
        initTitle()
        initClick()
        initObserve()
        if (key == null){
            viewModel.getDefAccount()
        }else{
            viewModel.getDefAccount(key)
        }
    }

    private fun initObserve() {
        viewModel.account.observe(this) {
            dataBinding.account = it
        }
        viewModel.id.observe(this) {
            dataBinding.id = it
        }
    }

    private fun initClick() {
        dataBinding.include18.ivHeadBack.setOnClickListener {
            setResult(200)
            this.finish()
        }
        dataBinding.llAdminName.setOnClickListener {
            MaterialDialog.Builder(this)
                .title(R.string.IdentityName)
                .inputType(InputType.TYPE_CLASS_TEXT)
                .input(
                    viewModel.accountSchema?.account,
                    ""
                ) { _, input ->
                    if (input.toString().isNotEmpty()) {
                        viewModel.updateAccountName(input.toString())
                    } else {
                        ToastUtils.showShort(R.string.pleaseEnterContent)
                    }
                }
                .positiveText(R.string.confirm)
                .negativeText(R.string.cancel)
                .cancelable(false)
                .show()
        }
    }

    private fun initTitle() {
        dataBinding.include18.tvHeadTitles.text = getString(R.string.adminDetails)
    }
}